package com.smmisia.auth.utls;

import org.lionsoul.ip2region.xdb.Searcher;
import org.springframework.stereotype.Component;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

@Component
public class IpUtil {

    private Searcher getSearcher() throws Exception {
        Path path = Paths.get(IpUtil.class.getClassLoader()
                .getResource("ip2region.xdb").toURI());
        byte[] bytes = Files.readAllBytes(path);
        return Searcher.newWithBuffer(bytes);
    }

    public String getAddress(String ip){
        try {
            Searcher searcher = getSearcher();
            return searcher.search(ip);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "未知1";
    }
}
